<template>
  <el-dialog
    :visible.sync="isShow"
    :title="mode === 'add' ? '新增部门' : '编辑部门'"
    width="50%"
    @close="dialogClose"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model.trim="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" @focus="selectFocus">
          <el-option
            v-for="i in userList"
            :key="i.id"
            :label="i.username"
            :value="i.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model.trim="form.introduce" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {
  sysUserSimple,
  companyDepartmentPost,
  companyDepartmentPut
} from '@/api/departments.js'
export default {
  name: 'DialogItem',
  props: {
    initList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      isShow: false,
      userList: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              let list = ''
              if (this.mode === 'add') {
                // 新增
                list = this.initList.filter((item) => {
                  return item.pid === this.departmentInfo.id
                })
              } else {
                // 编辑
                list = this.initList.filter((item) => {
                  return (
                    item.pid === this.departmentInfo.pid &&
                    item.id !== this.departmentInfo.id
                  )
                })
              }
              const res = list.some((item) => {
                return item.name === value
              })
              res
                ? callback(new Error('请不要输入重复的部门名字：' + value))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              let res = ''
              if (this.mode === 'add') {
                // 新增
                res = this.initList.some((item) => {
                  return item.code === value
                })
              } else {
                // 编辑
                res = this.initList.some((item) => {
                  return (
                    item.code === value && item.id !== this.departmentInfo.id
                  )
                })
              }
              res
                ? callback(new Error('请不要输入重复的部门编码：' + value))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        manager: [{ required: true, message: '必填', trigger: 'blur' }],
        introduce: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      departmentInfo: '',
      mode: 'add'
    }
  },
  mounted() {
    this.$bus.$on('show', (bol, item, mode = 'add') => {
      this.isShow = bol
      this.departmentInfo = item
      this.mode = mode
      if (mode === 'edit') {
        // 深拷贝
        this.form = JSON.parse(JSON.stringify(item))
      }
    })
  },
  beforeDestroy() {
    this.$bus.$off('show')
  },
  methods: {
    // 获取部门负责人数据
    async selectFocus() {
      if (this.userList.length === 0) {
        const res = await sysUserSimple()
        this.userList = res.data
      }
    },
    // 弹框确定点击事件
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            // 新增
            this.form.pid = this.departmentInfo.id
            await companyDepartmentPost(this.form)
          } else {
            // 编辑
            await companyDepartmentPut(this.form)
          }
          this.$message.success(
            this.mode === 'add' ? '新增部门成功' : '编辑部门成功'
          )
          this.isShow = false
          this.$emit('getDepartment')
        }
      })
    },
    // 关闭弹框
    dialogClose() {
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    }
  }
}
</script>

<style></style>
